<template>
  <d2-container>
    <el-form :model="FormSearch" status-icon label-width="100px" class="demo-ruleForm">
      <el-row :gutter="24">
        <el-col :span="5">
          <el-form-item label="条件查找" prop="good">
            <el-col :span="5">
              <el-date-picker
                style="width: 150px"
                v-model="FormSearch.timea"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="开始时间">
              </el-date-picker>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-date-picker
            style="width: 150px"
            v-model="FormSearch.timeb"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="结束时间">
          </el-date-picker>
        </el-col>
        <el-col :span="4">
          <el-select v-model="departmentId" placeholder="请选择科室">
            <el-option
              v-for="item in department"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
            <el-select v-model="FormSearch.value1" placeholder="请选择订单类型">
              <el-option
                v-for="item in FormSearch.options1"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-col>
        <el-col :span="5">
          <el-button type="primary" @click="searchClick()">查询</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 数据表格 -->
    <d2-crud
      ref="d2Crud"
      edit-title="编辑"
      add-title="添加缴费项目"
      :columns="columns"
      :rowHandle="rowHandle"
      :data="data"
      :edit-template="editTemplate"
      :add-template="addTemplate"
      :add-rules="addRules"
      :form-options="formOptions"
      :pagination="page"
      :loading="loading"
      @dialog-open="handleDialogOpen"
      @row-edit="handleRowEdit"
      @row-add="handleRowAdd"
      @dialog-cancel="handleDialogCancel"
      @custom-emit-1="createBatch"
      @custom-emit-2="delGood"
      @pagination-current-change="paginationCurrentChange"
    >
    <!-- <el-button slot="header" style="margin-bottom: 5px;" @click="addRow">添加缴费项目</el-button> -->
    </d2-crud>
  </d2-container>
</template>

<script>
import {
  getGoodType,
  } from "@/api/goods/index";
import { orderKsTj, addPayItem, updatePayItem, deletePayItem } from "@/api/accountAdmin/index";
import util from "@/libs/util.js";

export default {
  data() {
    return {
      loading: false,
      departmentId: '',
      department: [],
      // 搜索数据
      FormSearch: {
        timea: "",
        timeb: "",
        value: "",
        value1: "",
        options: [
          { value: '', label: '全部' },
          { value: '0', label: '用户订餐' },
          { value: '1', label: '用户预约' },
          { value: '2', label: '员工' },
        ],
        options1: [
          { value: '', label: '全部' },
          { value: '0', label: '用户订餐' },
          { value: '1', label: '用户预约' },
          { value: '2', label: '员工订餐' },
          { value: '3', label: '员工现金' },
          { value: '4', label: '用户现金' },
        ],
      },
      // 字段名称
      columns: [
        {
          title: "科室",
          key: "department",
          align: "center"
        },
        {
          title: "订单数量",
          key: "count",
          align: "center"
        },
        {
          title: "总金额",
          key: "money",
          align: "center"
        },
        {
          title: "类型",
          key: "o_type1",
          align: "center"
        },
      ],
      // 列表数据
      data: [],
      // 操作列数据
      rowHandle: {
        align: "center",
        width: 100,
        custom: [
          {
            text: "详情",
            type: "primary",
            size: "mini",
            emit: "custom-emit-1",
          },
          /* {
            text: "删除",
            type: "danger",
            size: "mini",
            emit: "custom-emit-2",
            show (index, row) {
            if (row.type != 1) {
                return true
              }
              return false
            },
          }, */
        ],
        /* edit: {
          icon: "el-icon-edit",
          text: "编辑",
          size: "small"
        } */
      },
      addTemplate: {
        name: {
          title: "缴费名称",
          value: ""
        },
        price: {
          title: "单价",
          value: ""
        },
        charge: {
          title: "额外收费",
          value: ""
        },
        type: {
          title: "缴费类型",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '1', label: '物业费' },
              { value: '2', label: '其他(按单价面积计算)' },
            ],
            span: 24
          }
        },
        byIsArea: {
          title: "是否按面积收费",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '0', label: '否' },
              { value: '1', label: '是' },
            ],
            span: 24
          }
        },
      },
      addRules: {
        name: [ { required: true, message: '请输入缴费名称', trigger: 'blur' } ],
      },
      editTemplate: {
        name: {
          title: "缴费名称",
          value: ""
        },
        price: {
          title: "单价",
          value: ""
        },
        charge: {
          title: "额外收费",
          value: ""
        },
        by_is_area1: {
          title: "是否按面积收费",
          value: "",
          component: {
            name: 'el-select',
            options: [
              { value: '0', label: '否' },
              { value: '1', label: '是' },
            ],
            span: 24
          }
        },
      },
      formOptions: {
        labelWidth: "80px",
        labelPosition: "left",
        saveLoading: false
      },
      // 分页数据
      page: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      }
    };
  },
  created() {
    let params = {
      page: this.page.pageIndex,
      limit: this.page.pageSize
    };
    this.init(params);
  },
  mounted() {
    let params = {
      page: 1,
      limit: 9999999
    };
    params.type = 1
    params.shid = util.cookies.get('uuid')
      getGoodType(params)
        .then(res => {
          if (res.success) {
            let zopt = []
              res.rows.map(item => {
                let opt = {
                  value: item.name,
                  label: item.name
                }
                zopt.push(opt)
              })
              this.department = zopt
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
  },
  methods: {
    // 初始化身体数据列表
    init(params) {
      this.loading = true
      if (util.cookies.get('token') == 1) {
        params.shId = util.cookies.get('uuid');
      }
      orderKsTj(params)
        .then(res => {
          if (res.success) {
            res.rows.map(item => {
              if (item.count == undefined) {
                item.count = 0
              }
              if (item.money == undefined) {
                item.money = 0
              }
              if (item.o_type == 0) {
                item.o_type1 = '用户订餐'
              } else if (item.o_type == 1) {
                item.o_type1 = '用户预约'
              } else if (item.o_type == 2) {
                item.o_type1 = '员工订餐'
              } else if (item.o_type == 3) {
                item.o_type1 = '员工现金'
              } else if (item.o_type == 4) {
                item.o_type1 = '用户现金'
              } else {
                item.o_type1 = ''
              }
            })
            this.data = res.rows;
            this.page.total = res.total;
          }
          this.loading = false
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err);
        });
    },

    // 普通的新增
    addRow() {
      this.$refs.d2Crud.showDialog({
        mode: "add"
      });
    },

    // 新增成功
    handleRowAdd(row, done) {
      this.formOptions.saveLoading = true;
      let params = row;
      addPayItem(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }

          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize,
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 分页数改变，调用
    paginationCurrentChange(pageIndex) {
      let params = {
        limit: this.page.pageSize,
      };
      params.page = this.page.pageIndex = pageIndex;
      params.eaddress = this.FormSearch.value;
      params.otype = this.FormSearch.value1;
      params.timea = this.FormSearch.timea;
      params.timeb = this.FormSearch.timeb;
      this.init(params);
    },

    // 条件查询搜索
    searchClick() {
      let params = {
        page: 1,
        limit: 10
      };
      params.address = this.FormSearch.value;
      params.otype = this.FormSearch.value1;
      params.timea = this.FormSearch.timea;
      params.timeb = this.FormSearch.timeb;
      this.init(params);
    },

    // 重置查询
    resetForm() {
      this.FormSearch.good = "";
      let params = {
        page: 1,
        limit: 10
      };
      this.init(params);
    },

    // 点击编辑时 mode 为编辑或者添加，row为点击这一行数据
    handleDialogOpen({ mode, row }) {
    },

    // 编辑提交
    handleRowEdit({ index, row }, done) {
      // 保存按钮的loading状态
      this.formOptions.saveLoading = true;
      let params = {};
      params.id = row.id;
      params.name = row.name;
      params.price = row.price;
      params.charge = row.charge;
      if (!isNaN(row.by_is_area1)) {
        params.byIsArea = row.by_is_area1
      } else {
        params.byIsArea = row.by_is_area
      }
      console.log(row)
      updatePayItem(params)
        .then(res => {
          if (res.success) {
            this.$message({
              message: "编辑成功",
              type: "success"
            });
          }
          // done可以传入一个对象来修改提交的某个字段
          done();
          // 保存按钮的loading状态
          this.formOptions.saveLoading = false;
          // 重新加载列表
          let params = {
            page: this.page.pageIndex,
            limit: this.page.pageSize
          };
          this.init(params);
        })
        .catch(err => {
          this.$message.error(err);
        });
    },

    // 取消编辑
    handleDialogCancel(done) {
      /* this.$message({
        message: "取消编辑",
        type: "warning"
      }); */
      done();
    },

    // 生成批次
    createBatch({ index, row }) {
      this.$router.push({
        path: "/reportForm/departmentInfo",
        query: {
          otype: row.o_type,
          address: row.department,
        }
      });
    },

    // 删除分组
    delGood({ index, row }) {
      let params = {
        id: row.id,
      }
      this.$confirm(`此操作将删除缴费项目, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deletePayItem(params)
            .then(res => {
              if (res.success) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
                // 重新加载列表
                let params = {
                  page: this.page.pageIndex,
                  limit: this.page.pageSize,
                };
                this.init(params);
              }
            })
            .catch(err => {
              this.$message.error(err);
            });
        })
        .catch(err => {});
    },
  }
};
</script>
<style lang="scss" scoped>
</style>
